package cn.ecnu.controller.forum;

import cn.ecnu.common.utils.R;
import cn.ecnu.param.forum.PostParameter;
import cn.ecnu.pojo.forum.Post;
import cn.ecnu.service.forum.ForumService;
import cn.ecnu.service.forum.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/circle/post")
public class PostController {

    @Autowired
    private PostService postService;

    @Autowired
    private ForumService forumService;

    private static int PAGE_SIZE = 2; // 页的大小

    // 获取默认的校园圈列表, 按照时间排序
    // 按照默认 页数获取校园圈列表
    @GetMapping("/getPostListByForumId/{forumId}")
    public R getPostListByForumIdPage(@PathVariable("forumId") Integer forumId, PostParameter parameter) {
        HashMap<String, Object> result = new HashMap<>(); // 返回结果
        Integer pageNum = 1;
        if (parameter != null && parameter.getPageNum() != null)  pageNum = parameter.getPageNum();
        List<Post> posts = postService.getPostByForumIdUsePageOrderByTime(forumId, pageNum, PAGE_SIZE);
        // 是否存在下一个
        if (posts != null && posts.size() == PAGE_SIZE) {
            result.put("has_next", true); // 存在下一页数据
            result.put("next_url", new PostParameter("getPostListByForumId", forumId, pageNum + 1).toUrl()); // 下一页数据的url
        } else {
            result.put("has_next", false);
        }
        R r = R.ok().put("data", posts);
        r.putAll(result);
        return r;
    }


    // 按照热度获取校园圈列表
    @GetMapping("/getPostListByForumIdOrderByHot/{forumId}")
    public R getPostListByForumIdOrderByHot(@PathVariable("forumId") Integer forumId, PostParameter parameter) {
        HashMap<String, Object> result = new HashMap<>(); // 返回结果
        Integer pageNum = 1;
        if (parameter != null && parameter.getPageNum() != null)  pageNum = parameter.getPageNum();
        List<Post> posts = postService.getPostByForumIdUsePageOrderByHot(forumId, pageNum, PAGE_SIZE);
        // 是否存在下一个
        if (posts != null && posts.size() == PAGE_SIZE) {
            result.put("has_next", true); // 存在下一页数据
            result.put("next_url", new PostParameter("getPostListByForumIdOrderByHot", forumId, pageNum + 1).toUrl()); // 下一页数据的url
        } else {
            result.put("has_next", false);
        }
        R r = R.ok().put("data", posts);
        r.putAll(result);
        return r;
    }

    // 获取默认的校园圈列表, 按照时间排序
    // 按照默认 页数获取校园圈列表
    @GetMapping("/getPostListByUserId/{userId}")
    public R getPostListByUserIdPage(@PathVariable("userId") Integer userId, PostParameter parameter) {
        HashMap<String, Object> result = new HashMap<>(); // 返回结果
        Integer pageNum = 1;
        if (parameter != null && parameter.getPageNum() != null)  pageNum = parameter.getPageNum();
        List<Post> posts = postService.getPostByUserIdUsePageOrderByTime(userId, pageNum, PAGE_SIZE);
        // 是否存在下一个
        if (posts != null && posts.size() == PAGE_SIZE) {
            result.put("has_next", true); // 存在下一页数据
            result.put("next_url", new PostParameter("getPostListByUserId", userId, pageNum + 1).toUrl()); // 下一页数据的url
        } else {
            result.put("has_next", false);
        }
        R r = R.ok().put("data", posts);
        r.putAll(result);
        return r;
    }

    // 按照热度获取校园圈列表
    // 按照热度和用户id获取校园圈列表
    @GetMapping("/getPostListByUserIdOrderByHot/{userId}")
    public R getPostListByUserIdOrderByHot(@PathVariable("userId") Integer userId, PostParameter parameter) {
        HashMap<String, Object> result = new HashMap<>(); // 返回结果
        Integer pageNum = 1;
        if (parameter != null && parameter.getPageNum() != null)  pageNum = parameter.getPageNum();
        List<Post> posts = postService.getPostByUserIdUsePageOrderByHot(userId, pageNum, PAGE_SIZE);
        // 是否存在下一个
        if (posts != null && posts.size() == PAGE_SIZE) {
            result.put("has_next", true); // 存在下一页数据
            result.put("next_url", new PostParameter("getPostListByUserIdOrderByHot", userId, pageNum + 1).toUrl()); // 下一页数据的url
        } else {
            result.put("has_next", false);
        }
        R r = R.ok().put("data", posts);
        r.putAll(result);
        return r;
    }

    @GetMapping("/getPostListByKeyword/{keyword}")
    public R getPostListByKeyword(@PathVariable String keyword) {
        List<Post> posts = postService.getPostByTagsAndContent(keyword);
        return R.ok().put("data", posts);
    }

    @PostMapping("/insertPost")
    public R insertPost(@RequestBody Post post) {
        post.setPostTime(new Date());
        postService.insertPost(post);
        return R.ok().put("data", post);
    }

    @GetMapping("/deletePostById/{postId}")
    public R deletePostById(@PathVariable("postId") Integer postId) {
        postService.deletePostById(postId);
        return R.ok();
    }

    @PostMapping("/updatePostById")
    public R updatePostById(@RequestBody Post post) {
        post.setPostTime(new Date());
        postService.updatePostById(post);
        return R.ok();
    }

    @GetMapping("/getPostById/{id}")
    public R getPostById(@PathVariable("id") Integer id) {
        try {
            Post post = postService.getPostById(id);
            post.setHot(post.getHot() + 1);
            postService.updatePostById(post);
            return R.ok().put("data", post);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error();
    }




}
